#include <string.h>
#include <GL/gl.h>
#include "qzeroicon.h"

QZeroIcon::QZeroIcon(const QZeroIconProperties& prop)
{
  prop_ = prop;
  down_ = false;
  highlighted_ = false;
  alpha_ = 1.0f;
}

QZeroIcon::QZeroIcon(const QZeroIcon& icon)
{
  _w = icon._w;
  _x = icon._x;
  _y = icon._y;
  _h = icon._h;
  bbox = icon.bbox;
  theTex = icon.theTex;
  prop_ = icon.prop_;
  highlighted_ = icon.highlighted_;
  down_ = icon.down_;
  alpha_ = icon.alpha_;
}

QZeroIcon::~QZeroIcon()
{

}


bool QZeroIcon::Init(int x,
                     int y,
                     int w,
                     int h)
{
  int delta = 5;
  _w=w;
  _x=x;
  _y=y;
  _h=h;
  bbox.ulcx = x - delta;
  bbox.lrcx = x + w + delta;
  bbox.ulcy = y + h + delta;
  bbox.lrcy = y - delta;

  if ( !theTex.Load( prop_.filename() ) )
  {
    return(false);
  }
  highlighted_ = false;
  down_ = false;

  return(true);
}


void QZeroIcon::draw(unsigned int width, unsigned int height, int z)
{
  glDisable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);					//Select the projection matrix
  glPushMatrix();									//Store the projection matrix
  glLoadIdentity();								//Reset the projection matrix
  glOrtho(0,width,0,height,-1,1);	//Set up an ortho screen
  glMatrixMode(GL_MODELVIEW);						//Select the modelview matrix
  glPushMatrix();
  glLoadIdentity();
  glColor4f( 1.0, 1.0, 1.0, 1.0);
  glEnable(GL_TEXTURE_2D);

  theTex.Bind();
  float d = 0.1f;

  if (highlighted_)
  {
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    glLineWidth(2.0f);
    glBegin(GL_QUADS);
      glColor4f(1.0f, 1.0f, 1.0f, alpha_);
      glVertex3f(bbox.ulcx, bbox.ulcy, z-d);
      glVertex3f(bbox.lrcx, bbox.ulcy, z-d);
      glVertex3f(bbox.lrcx, bbox.lrcy, z-d);
      glVertex3f(bbox.ulcx, bbox.lrcy, z-d);
    glEnd();
    glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    glDisable(GL_BLEND);
  }

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  glColor4f(1.0f, 1.0f, 1.0f, alpha_);
  glBegin(GL_QUADS);
    glNormal3f( 0.0f, -1.0f, 0.0f);
    glTexCoord2f( 0, 0 ); glVertex3f( _x     ,  _y    , z );
    glTexCoord2f( 1, 0 ); glVertex3f( _x + _w,  _y    , z );
    glTexCoord2f( 1, 1 ); glVertex3f( _x + _w,  _y + _h, z );
    glTexCoord2f( 0 ,1 ); glVertex3f( _x     ,  _y + _h, z );
  glEnd();
  glDisable(GL_BLEND);

  if (down_)
  {
    glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    glColor4f(1.0f, 1.0f, 1.0f, 0.75f);
    glLineWidth(2.0f);
    glBegin(GL_QUADS);
      glVertex3f(bbox.ulcx, bbox.ulcy, z+d);
      glVertex3f(bbox.lrcx, bbox.ulcy, z+d);
      glVertex3f(bbox.lrcx, bbox.lrcy, z+d);
      glVertex3f(bbox.ulcx, bbox.lrcy, z+d);
    glEnd();
    glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    glDisable(GL_BLEND);
  }

  glPopMatrix();
  glMatrixMode(GL_PROJECTION);					//Select the projection matrix
  glPopMatrix();									//Restore the old projection matrix
  glMatrixMode(GL_MODELVIEW);						//Select the modelview matrix
  glDisable(GL_BLEND);
  glEnable(GL_DEPTH_TEST);
}

bool QZeroIcon::highlight(int x, int y)
{
  if (mouseOverBox(x, y))
  {
    highlighted_ = true;
    return(true);
  }
  highlighted_ = false;

  return(false);
}


bool QZeroIcon::mouseOverBox(int x, int y)
{
  if ((x>=bbox.ulcx) && (x<=bbox.lrcx) && (y<=bbox.ulcy) && (y>=bbox.lrcy))
  {
    return( true );
  }

  return( false );
}

void QZeroIcon::press(int x, int y)
{

  if (mouseOverBox(x, y) && !down_)
  {
    down_ = true;
  }
  else if (mouseOverBox(x, y) && down_)
  {
    down_ = false;
  }

}

void QZeroIcon::release(int x, int y)
{
  if (!prop_.sticky())
  {
    if (mouseOverBox(x, y) && down_)
    {
      down_ = false;
    }
  }

}


